package com.redisdemo.File;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * <h3>redisDemo</h3>
 * <p>线程文件读取类</p>
 *
 * @author gdw
 * @since 2024-07-02 15:14:58
 */
public class FileReadTask implements Runnable{
    private final String filePath;
    private final long start;
    private final long size;

    public FileReadTask(String filePath, long start, long size) {
        this.filePath = filePath;
        this.start = start;
        this.size = size;
    }
    @Override
    public void run() {
        try (RandomAccessFile file = new RandomAccessFile(filePath, "r");
             FileChannel channel = file.getChannel()) {

            MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, start, size);

            byte[] bytes = new byte[(int) size];
            buffer.get(bytes);
            // // 处理读取的数据，例如打印出来
            // System.out.println(new String(bytes));
            // 处理读取的数据，例如打印出来
            System.out.println(Thread.currentThread().getName() + ": Read " + size + " bytes.");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
